//
//  ChildHoodMessage.m
//  ChildHoodStemp
//
//  Created by xuzuotao on 13-9-18.
//
//

#import "ChildHoodMessage.h"
#import "ChildHoodMemory.h"
#import "Snsp.pb.h"
@interface ChildHoodMessage()

-(NSString*)getMessageContent;
-(NSDate*)getMessageTime;
-(BOOL)getMemoryValid;
-(NSString*)getSenderName;
-(NSString*)getSenderPortraitImageUri;
-(NSString*)getSenderPortraitImageNextUri;
//-(NSData*)  getMessageDtata;
-(SNSPMaterialType)getMessageType;
-(uint32_t)getMemoryID;
-(SNSPMessageType)getType;
-(int64_t)getMessageSerDBID;
@end

@implementation ChildHoodMessage

@synthesize messageContent;
@synthesize messageTime;
@synthesize memoryValid;
@synthesize senderName;
@synthesize senderPortraitImageUri;
@synthesize senderPortraitImageNextUri;
//@synthesize messageData;
@synthesize messageType;
@synthesize type;
@synthesize messageSerDBID;
@synthesize memoryID;
@synthesize messageRoomID;
@synthesize title;
@synthesize relativeType;
@synthesize fileUri;
@synthesize nextFileUri;
@synthesize snspMessage = _snspMessage;


@synthesize totalCount = _totalCount;
@synthesize messageFilePath = _messageFilePath;

@synthesize messageDBID = _messageDBID;

@synthesize messageStatus = _messageStatus;

@synthesize  materialCount = _materialCount;

@synthesize isShowing = _isShowing;
@synthesize isRead = _isRead;
@synthesize isPlayed = _isPlayed;
@synthesize isDownloaded = _isDownloaded;
@synthesize isMine = _isMine;


-(id)initWithSNSMessage:(SNSPMessage*) snspMessage
{
    if(self = [super init])
    {
        _snspMessage = snspMessage;
        if(snspMessage.attachmentFileKeyOrUrlsList.count || snspMessage.miniAttachmentFileUrlsList.count)
        {
            _materialCount = 1;
        }else
            _materialCount = 0;
        
        _isShowing = YES;
        _isRead    = NO;
        _childHoodMemory = nil; //to load the childhood memory from data if it can be loaded from database;
        

    }
    return self;
    
}

-(void)dealloc
{
    
}

-(int64_t)getMessageSerDBID
{
    if (_snspMessage.hasMessageId) {
        return _snspMessage.messageId;
    }
    return -1;
}

-(int64_t)getMessageRoomID
{
    if(_snspMessage.hasRoomId)
    {
        return _snspMessage.roomId;
    }
    return -1;
}

-(int64_t)getMessageGMTTime
{
    int64_t retTime;
    
    if(_snspMessage.hasCreateTime)
    {
        retTime = _snspMessage.createTime;
     }else
    {
        retTime = time(NULL)* 1000;
    }
    

    return retTime;
}

-(int32_t)getRelativeType
{
    if(_snspMessage.hasRelativeType)
    {
        return _snspMessage.relativeType;
    }
    return SNSPRelativeTypeKSelf;
}


//-(NSString*)getFileUri
//{
//    /*if(_snspMessage.hasMultimedia)
//    {
//        if(_snspMessage.multimedia.hasFileUri)
//        {
//            return _snspMessage.multimedia.fileUri;
//        }
//    }*/
//    
//    /**/
//    
//    if(self.snspMessage.attachmentFileKeysList)
//    {
//    
//        return [NSString stringWithFormat:@"%@%@",[self.snspMessage.attachmentFileKeysList objectAtIndex:0], KMINIIMAGEPARAMETER];
//    }
//    
//    if(self.snspMessage.miniAttachmentFileUrlsList.count)
//    {
//        return [self.snspMessage.miniAttachmentFileUrlsList objectAtIndex:0];
//    }
//    
//    
//    if(self.snspMessage.attachmentFileKeyOrUrlsList.count)
//    {
//        return [self.snspMessage.attachmentFileKeyOrUrlsList objectAtIndex:0];
//    }
//    
//    
//    return nil;
//}
-(NSString*)getFileNextUri
{
    /*if(_snspMessage.hasMultimedia)
    {
        if(_snspMessage.multimedia.hasNextFileUri)
        {
            return _snspMessage.multimedia.nextFileUri;
        }
        if(_snspMessage.multimedia.hasFileUri)
        {
            return _snspMessage.multimedia.fileUri;
        }
    }*/
    
    
    
    
    
    if(self.snspMessage.attachmentFileKeysList)
    {
        return [self.snspMessage.attachmentFileKeysList objectAtIndex:0];
    }
    

    if(self.snspMessage.attachmentFileKeyOrUrlsList.count)
     {
     return [self.snspMessage.attachmentFileKeyOrUrlsList objectAtIndex:0];
     }
     
     if(self.snspMessage.miniAttachmentFileUrlsList.count)
     {
     return [self.snspMessage.miniAttachmentFileUrlsList objectAtIndex:0];
     }
    
    
    return nil;
}


-(uint32_t)getMemoryID
{
    if(_snspMessage.hasMemoryId)
    {
        return _snspMessage.memoryId;
    }
    return -1;
}

-(NSString*)getMessageContent
{
    NSString* str = nil;
//    if(_snspMessage.hasContent)
//    {
        if(_snspMessage.hasMaterialType)
        {
            switch (_snspMessage.materialType) {
                case SNSPMaterialTypeKImage:
                {
                    str = @"图片";
                    break;
                }
                case SNSPMaterialTypeKAudio:
                {
                    str = @"声音";
                    break;
                }
                case SNSPMaterialTypeKVideo:
                {
                    str = @"视频";
                    break;
                }
                case SNSPMaterialTypeKMessage:
                {
                    return [[NSString alloc]initWithData:_snspMessage.content encoding:NSUTF8StringEncoding];
                    break;
                }
                case SNSPShake:
                {
                    return [[NSString alloc]initWithData:_snspMessage.content encoding:NSUTF8StringEncoding];
                    break;
                }
                case SNSRedPacket:
                {
                    return [[NSString alloc]initWithData:_snspMessage.content encoding:NSUTF8StringEncoding];
                    break;
                }
                default:
                    str = @"测试内容";
                    break;
            }
        
        }else
        {
            str = @"unknown msg1111!";
        }

    return str;

}

-(void)setSNSPMessageData:(NSData*)data
{
    _snspMessage = nil;
    _snspMessage = [SNSPMessage parseFromData:data];
}

-(NSString*)getTitle
{
    switch (_snspMessage.relativeType) {
        case SNSPRelativeTypeKSelf:
            return     _childHoodMemory.profileTitle;
            break;
        case SNSPRelativeTypeKFather:
            return     @"爸爸";
            break;

        case SNSPRelativeTypeKMother:
            return     @"妈妈";
            break;
        case SNSPRelativeTypeKGrandFather:
            return     @"爷爷";
            break;
        case SNSPRelativeTypeKGrandMother:
            return     @"奶奶";
            break;
        case SNSPRelativeTypeKOuterGrandFather:
            return     @"姥爷";
            break;
        case SNSPRelativeTypeKOuterGrandMother:
            return     @"姥姥";
            break;
        case SNSPRelativeTypeKUncle:
            return     @"叔叔";
            break;
        case SNSPRelativeTypeKAunt:
            return     @"阿姨";
            break;
           
        default:
            break;
    }
    return @"家属";
}

-(NSDate*)getMessageTime
{
    int64_t retTime;
    
    if(_snspMessage.hasCreateTime)
    {
        retTime = _snspMessage.createTime;
    }else
    {
        retTime = time(NULL);
    }

//    NSDate* datenow = [NSDate date];
//    NSTimeZone *zone = [NSTimeZone systemTimeZone];
//    
//    NSInteger interval = [zone secondsFromGMTForDate:datenow];
//    retTime+= interval;
//    NSDate *dd = [NSDate dateWithTimeIntervalSince1970:retTime / 1000];
   
   return [NSDate dateWithTimeIntervalSince1970:retTime / 1000.0];
    
}

-(BOOL)getMemoryValid
{
    return (_childHoodMemory != nil);
}

-(NSString*)getSenderName
{
    if(_childHoodMemory)
    {
        if(_childHoodMemory.snspMemory.hasName)
            return _childHoodMemory.snspMemory.name;
    }
    return @"";
}
- (NSString*)getSenderGroupName{
    if(_childHoodMemory)
    {
        if(_childHoodMemory.snspMemory.groupName)
            return _childHoodMemory.snspMemory.groupName;
    }
    return @"";
}

- (ContactsGroupType)getSenderGroupType{
    if(_childHoodMemory)
    {
        if(_childHoodMemory )
            return _childHoodMemory.contactGroup;
    }
    return 0;
}

-(NSString*)getSenderPortraitImageUri
{
    if(_childHoodMemory)
    {
    
        if(_childHoodMemory)
            return  _childHoodMemory.profilePictureUri;

    }
//    return @"headImage_boy.png"; //return the stranger's picture
    return @"testHeader";
}

-(NSString*)getSenderPortraitImageNextUri
{
    if(_childHoodMemory)
    {
        
        if(_childHoodMemory)
            return  _childHoodMemory.profilePictureNextUri;
    }
    return @"testHeader"; //return the stranger's picture
}

//-(NSData*)  getMessageDtata
//{
//    if(_snspMessage.hasContent)
//        return _snspMessage.content;
//    return nil;
//}

-(SNSPMaterialType)getMessageType
{
    if(_snspMessage.hasMaterialType)
    {
        return _snspMessage.materialType;
    }
    
   
    return SNSPMaterialTypeKMessage;
}

-(SNSPMessageType)getType
{
    if (_snspMessage.hasMessageType) {
        return _snspMessage.messageType;
    }
    return SNSPMessageTypeKMessageType;
}

-(void)setChildHoodMemory:(ChildHoodMemory*)childhoodMemory
{
    if(_childHoodMemory==childhoodMemory)
    {
        return;
    }
    _childHoodMemory = nil;
    _childHoodMemory = childhoodMemory;
}
-(BOOL)isMemroyInValid
{
    if(_childHoodMemory == nil)
    {
        return YES;
    }
    return NO;
}

@end
